import 'package:flutter/material.dart';

class PageViewLoop extends StatefulWidget {
  const PageViewLoop({super.key});
  @override
  State<PageViewLoop> createState() => _PageViewPageState();
}
class _PageViewPageState extends State<PageViewLoop> {
  List<Widget> pageList = [];
  @override
  void initState() {
    List listData = [
      {
        "imageUrl": 'https://www.itying.com/images/flutter/1.png',
      },
      {
        "imageUrl": 'https://www.itying.com/images/flutter/2.png',
      },
      {
        "imageUrl": 'https://www.itying.com/images/flutter/3.png',
      }
    ];
    for (int i = 0; i < listData.length; ++i) {
      pageList.add(PicturePage(       //集合 里面就是加载了3个 图片的View
        url: listData[i]["imageUrl"],
      ));
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("pageview演示"),
        ),
      //  body: ListView(
        body: Column(
          children: [Swiper(pageList: pageList)],     // 把图片集合传递给一个自定义组件Swipper
        ));
  }
}
//Swiper组件
class Swiper extends StatefulWidget {
  final double width;
  final double height;
  final List<Widget> pageList;
  const Swiper(
      {super.key,
        this.width = double.infinity,
        this.height = 200,
        required this.pageList});
  @override
  State<Swiper> createState() => _SwiperState();
}
class _SwiperState extends State<Swiper> {
  int _currentPageIndex = 0;
  @override
  void initState() {
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
       // SizedBox(
        SizedBox(
          width: double.infinity,
          height: 200,
         // padding: EdgeInsets.only(left: 10,right: 10),
          child: PageView.builder(
              onPageChanged: (int index) {          //滑动一千克PageView,获取当前的index
                setState(() {
                  _currentPageIndex = index % (widget.pageList.length);     // 通过取余运算，获取当前 _currentPageIndex 的余数，作为ImageView对应的Banner 的index
                });
              },
              itemCount: 10000,
              itemBuilder: (context, index) {      //创建了一千个 PageView
                return widget.pageList[index % (widget.pageList.length)];
              }),
        ),
        Positioned(
          bottom: 10,
          left: 0,
          right: 0,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: List.generate(widget.pageList.length, (i) {           //遍历创建三个点，以 当前banner 对应的index 作为选中的item 图标，着重凸显显示。
              return Container(
                margin: const EdgeInsets.fromLTRB(2, 0, 2, 0),
                width: 10,
                height: 10,
                decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    color: _currentPageIndex == i ? Colors.blue : Colors.grey),
              );
            }).toList(),
          ),
        ),
      ],
    );
  }
}
//PicturePage 图片页面
class PicturePage extends StatefulWidget {
  final String url;
  final double width;
  final double height;
  const PicturePage(
      {super.key,
        required this.url,
        this.width = double.infinity,
        this.height = 200});
  @override
  State<PicturePage> createState() => _PicturePageState();
}
class _PicturePageState extends State<PicturePage> {
  @override
  Widget build(BuildContext context) {
    print(widget.url);
    return Container(
      width: widget.width,
      height: widget.height,
      padding: EdgeInsets.only(left: 10,right: 10),
      child: ClipRRect(
        borderRadius: BorderRadius.all(Radius.circular(6)),
        child:  Image.network(widget.url, fit: BoxFit.cover),
      )


    );
  }
}